import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';

void main() => runApp(RootApp());

// 定义根目录Wigdget
class RootApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 根视图
    return MaterialApp(
      // theme: ThemeData(primaryColor: Colors.white),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
// 定义计时 ms
  double _totalTime = 10000;
  double _progressTime = 1000;

  // 边框阴影的宽度
  double _borderWidth = 2.0;
  // 一个计时器
  Timer _timer;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 100ms执行一次
    _timer = Timer.periodic(Duration(milliseconds: 100), (timer) {
      _progressTime += 100;
      if (_progressTime % 2000 == 0) {
        if (_borderWidth == 2.0) {
          _borderWidth = 18.0;
        } else {
          _borderWidth = 2.0;
        }
      }
      if (_progressTime > _totalTime) {
        // 停止计时器
        _timer.cancel();
      }
      _streamController.add(_progressTime);
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("textButton")),
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            alignment: Alignment.center,
            children: [
              // 第一部分 照片背景
              buildBgWiget(),
              // 第二部分 高斯模糊
              buildBlurWidget(),
              // 第三部分 倒计时圆圈
              buildTimeWidget()
            ],
          )),
    );
  }

  // 创建一个流控制器
  StreamController<double> _streamController = new StreamController();

  buildTimeWidget() {
    // 用户局部更新数据
    return StreamBuilder(
      stream: _streamController.stream,
      initialData: 0.0,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        return AnimatedContainer(
          duration: Duration(seconds: 2),
          // 背景与边框
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey[200], width: 2.0),
            borderRadius: BorderRadius.all(
              Radius.circular(40),
            ),
            boxShadow: [
              BoxShadow(color: Colors.white, blurRadius: _borderWidth)
            ],
          ),

          child: Stack(
            alignment: Alignment.center,
            children: [
              SizedBox(
                width: 66,
                height: 66,
                child: CircularProgressIndicator(
                  backgroundColor: Colors.grey,
                  value: snapshot.data / _totalTime,
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
                ),
              ),
              Text(
                "${(snapshot.data / 1000).toInt()}",
                style: TextStyle(
                  fontSize: 33,
                  color: Colors.blue,
                  fontWeight: FontWeight.bold,
                ),
              )
            ],
          ),
        );
      },
    );
  }

  buildBlurWidget() {
    return BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 3, sigmaY: 3),
      child: Container(color: Colors.white.withOpacity(0.06)),
    );
  }

  buildBgWiget() {
    return Positioned.fill(
        child: Image.asset("images/3.0/bg.jpg", fit: BoxFit.fill));
  }
}
